/**
 *  管理员管理数据模块
 */
import axios from "../../utils/axios"
import {Message} from "element-ui"

export default {
    namespaced: true,
    state: {
        managerList: [],
        managerTotal: 0
    },
    mutations: {
        initManagerListMutations(state,list) {
            state.managerList = list
        },
        initMangerCountMutations(state,count) {
            state.managerTotal = count
        }
    },
    actions: {
        initManagerListActions({commit},pageInfo={page:1,size:5}) {
            // 1、初始化管理员数组
            axios.get("userlist",{params:pageInfo}).then(response => {
                // console.log(response, "管理员数组")
                commit('initManagerListMutations',response.list)
            })
            // 2、初始化管理员数量
            axios.get("usercount").then(response => {
                commit("initMangerCountMutations",response.list[0].total)
            })
        },
        addManagerActions({dispatch},manager) {
            axios.post("/useradd",manager).then(response => {
                if(response.code === 200) {
                    Message.success("管理员添加成功!")
                    dispatch("initManagerListActions",{size:2,page:1})
                }
            })
        },
        editManagerActions({dispatch},manager) {
            axios.post("/useredit",manager).then(response => {
                if(response.code === 200) {
                    Message.success("管理员修改成功!")
                    dispatch("initManagerListActions", {size: 2, page: 1})
                }
            })
        }
    },
    getters: {}
}